package com.czk.rocketmq.filter.tag;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.concurrent.TimeUnit;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/9/6 17:11
 */
public class Producer {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException {
        // 1.创建消息生产者producer，并制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        // 2.指定Nameserver地址
        producer.setNamesrvAddr("192.168.25.133:9876;192.168.25.134:9876");
        // 3.启动producer
        producer.start();
        // 4.创建消息对象，指定主题Topic、Tag和消息体
        for (int i = 0; i < 3; i++) {
            // 消息主题
            // 消息tag
            // 消息内容
            Message message = new Message("filter", "tag2", ("hello world " + i).getBytes());
            // 5.发送消息
            producer.send(message, new SendCallback() {
                // 发送成功的回调函数
                @Override
                public void onSuccess(SendResult sendResult) {
                    System.out.println("发送结果 " + sendResult);
                }

                // 发送失败的回调函数
                @Override
                public void onException(Throwable throwable) {
                    System.out.println("发送异常 " + throwable.getMessage());
                }
            });

            TimeUnit.SECONDS.sleep(1);
        }
        producer.shutdown();
    }
}
